<!-- 零售价调整 -->
<template>
  <el-dialog title="商品查询" :close-on-click-modal="false" :visible.sync="visible" top="-20px" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <div class="text item">
      <el-form inline>
        <el-form-item label="商品类别">
          <el-select v-model="dataForm.ctgId">
            <el-option v-for="item in this.ctgList" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" disabled>
            <el-button slot="append" @click="openDialog('supply')">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName" disabled>
            <el-button slot="append" @click="openDialog('brand')">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品代码">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="dataForm.spec"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getDataList()">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider content-position="left">查询数据</el-divider>

    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-button icon="el-icon-check" type="primary" @click="checkItemData">选 择</el-button>
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" border v-loading="dataListLoading" @select="onTableSelect" @selection-change="handleSelectionChange">
      <el-table-column key="1" type="selection" width="50"></el-table-column>
      <el-table-column key="2" prop="code" label="商品代码" width="140"></el-table-column>
      <el-table-column key="3" prop="supplyName" label="制造商名称"></el-table-column>
      <el-table-column key="4" prop="brandName" label="品种名称"></el-table-column>
      <el-table-column key="6" prop="price" label="零售价格" width="100"></el-table-column>
    </el-table>
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyId"></search-supply>
    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandId"></search-brand>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams, deduplication} from '@/util/tools'
  import { getGoodsPriceData,getGoodsListUseAdPrice } from '@/api/eims/goods'
  import { getCtgList } from '@/api/eims/ctg'

  import SearchSupply from '@/components/business/SearchSupply'
  import SearchBrand from '@/components/business/SearchBrand'
  export default {
    data() {
      return {
        visible: false,
        disabled: false,
        formData: [],
        selected: true,
        rowItem: {},
        dataForm: {
          ctgId: '',
          supplyId: '',
          supplyName: '',
          barcode: '',
          brandId: '',
          brandName: '',
          spec: ''
        },
        ctgList: [],
        dataList: [],
        dataListLoading: false,
        multipleSelection: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        supplyVisible: false,
        brandVisible: false
      }
    },
    components:{
      SearchSupply,
      SearchBrand
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init(data) {
        this.dataList = []
        this.getGoodsCtgList()
        this.visible = true
        this.pageIndex = 1
        this.multipleSelection = data.dataList
        this.dataForm.priceGroupId = data.priceGroupId
        this.getDataList()
      },
      //	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        getGoodsListUseAdPrice(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            let data = response.data.data.records
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
            this.$nextTick(() => {
              this.multipleSelection.forEach(item => {
                for (let i = 0; i < this.dataList.length; i++) {
                  let brandVal = item.brandId
                  let specVal = item.spec
                  let priceVal = item.price
                  if (brandVal == this.dataList[i].brandId && specVal ==this.dataList[i].spec && priceVal == this.dataList[i].price) {
                    this.$refs.singleTable.toggleRowSelection(this.dataList[i], true)
                  }
                }
              })
            })
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //  判断当前操作的属性是勾选还是取消勾选
      onTableSelect(rows,row) {
        this.selected = rows.length && rows.indexOf(row) !== -1
        this.rowItem = row
      },
      handleSelectionChange(val) {
        if(this.selected){
          val.forEach(item => {
            let result = this.multipleSelection.some(term => {
              if(term.brandId == item.brandId && term.spec == item.spec && term.price == item.price){
                return true
              }
            })
            if(!result){
              this.multipleSelection.push(item)
            }
          })
        }else{
          let that = this
          this.multipleSelection = this.multipleSelection.filter(function(item){
            return (item.brandId != that.rowItem.brandId || item.spec != that.rowItem.spec || item.price != that.rowItem.price)
          })
        }
      },
      //  选择并关闭
      checkItemData() {
        this.multipleSelection.forEach((item, index) => {
          item.oldPrice = (item.oldPrice || item.oldPrice == 0) ? item.oldPrice : item.price
        })
        this.visible = false
        this.$emit('updateForm', this.multipleSelection)
      },
      openDialog(type) {
        if(type == 'supply'){
          if(this.dataForm.ctgId){
            let data = { ctgId: this.dataForm.ctgId }
            this.supplyVisible = true
            this.$nextTick(() => {
              this.$refs.supplyDialog.init(data)
            })
          }else{
            this.$message.warning('请选择采购类型')
          }
        }else if(type == 'brand'){
          if(this.dataForm.supplyId){
            let data = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
            this.brandVisible = true
            this.$nextTick(() => {
              this.$refs.brandDialog.init(data)
            })
          }else{
            this.$message.warning('请选择制造商')
          }
        }
      },
      getSupplyId(data) {
        this.$set(this.dataForm,'supplyName',data.name)
        this.dataForm.supplyId = data.id
        this.getDataList()
      },
      getBrandId(data) {
        this.dataForm.brandId = data.id
        this.$set(this.dataForm,'brandName',data.name)
        this.getDataList()
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //  获取商品列表
      getGoodsCtgList() {
        getCtgList().then(response => {
          if(response.data.code == '0'){
            this.ctgList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>

<style>
</style>
